package vn.edu.ptithcm.pim.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import vn.edu.ptithcm.pim.dom.Employee;

public class EmployeeValidator implements Validator {

	public boolean supports(@SuppressWarnings("rawtypes") Class clazz) {
		return Employee.class.isAssignableFrom(clazz);
	}

	public void validate(Object target, Errors errors) {
		Employee employee = (Employee) target;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstname",
				"warning.required.employee.firstname");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastname",
				"warning.required.employee.lastname");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "visa",
				"warning.required.employee.visa");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateofbirth",
				"warning.required.employee.dateofbirth");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
				"warning.required.employee.email");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address",
				"warning.required.employee.address");

		if (employee.getDepartment().getDepartmentid().intValue() == 0) {
			errors.rejectValue("department",
					"warning.required.employee.department");
		}

		if (employee.getDiploma().getDiplomaid().intValue() == 0) {
			errors.rejectValue("diploma", "warning.required.employee.diploma");
		}

		if (employee.getRank().getRankid().intValue() == 0) {
			errors.rejectValue("rank", "warning.required.employee.rank");
		}

		if (employee.getUniversity().getUniversityid().intValue() == 0) {
			errors.rejectValue("university",
					"warning.required.employee.university");
		}
	}

}
